#include<stdio.h>
#include<stdlib.h>

int n;

void indexing(int *a,int n)
{
	int k,t;
	printf("Enter the element whose index is to be printed:");
	scanf("%d",&k);
	for (t=0;t<n;t++)
	{
		if (*(a+t)==k)
			{
				printf("%d\n",t);
				
			}
		exit;
	}
}

void pop(int *a,int n)
{
	int i,t,j;
	printf("Enter the index of the element to be popped:");
	scanf("%d",&i);
	printf("The element that pops out is %d\n",*(a+i));
	
	if (i<n)
	{
	for (t=i;t<n-1;t++)
	{
		*(a+t)=*(a+t+1);	
	}
	a=realloc((int*) a,(n-1)*sizeof(int));
	printf("The new array is :");
	for (j=0;j<n-1;j++)
	{
		printf(" %d ",*(a+j));	
	}
	printf("\n");
	}
	else
	{
		printf("Error: Index exceeds the length of the array\n");	
	}	
}

void reverse(int *a,int n)
{
	int b[n];
	int i;
	printf("The list in reverse order is:");
	for (i=0;i<n;i++)
	{
		b[i]=*(a+(n-1-i));
		printf(" %d ",b[i]);
	}
	printf("\n");
}

void count(int *a,int n)
{
	int k,t,count;
	printf("Enter the element whose count is to be determined:");
	scanf("%d",&k);
	count=0;
	for (t=0;t<n;t++)
	{
		if (*(a+t)==k)
			{
				count++;			
			}
			
	}
	printf("The count of %d is %d\n",k,count);

}

int insert(int *a,int n)
{
	int num,i,j,t;
	printf("Enter the element to be inserted:");
	scanf("%d",&num);
	printf("Enter the index position where the element is to inserted:");
	scanf("%d",&i);
	a=realloc((int*) a,(n+1)*sizeof(int));
	*(a+n)=*(a+n-1);
	for (j=n-1;j>=i+1;j--)
	{
		*(a+j)=*(a+j-1);
	}
	*(a+i)=num;
	printf("The new array is:");
	for (t=0;t<n+1;t++)
	{
		printf(" %d ",*(a+t));	
	}
	printf("\n");
}

void delete(int *a,int n)
{
	int t,x,j;
	printf("Enter the element to be removed:");
	scanf("%d",&x);
	t=0;
	while (*(a+t)!=x)
	{
		printf(" %d ",*(a+t));
		t++;	
	}
	for (j=t;j<n-1;j++)
	{
			*(a+j)=*(a+j+1);
			printf(" %d ",*(a+j));
	}
	a=realloc((int*) a,(n-1)*sizeof(int));
	printf("\n");
}

int append(int *a,int n)
{
	int num,t;
	printf("Enter the element to be appended:");
	scanf("%d",&num);
	a=realloc((int*) a,(n+1)*(sizeof(int)));
	*(a+n)=num;
	for (t=0;t<n+1;t++)
	{
		printf(" %d ",*(a+t));
	}
	printf("\n");
}

int extend(int* a,int n)
{
	int *b;
	int m,t,j;
	printf("Enter the size of the list to be appended:\n");
	scanf("%d",&m);
	b=malloc(m* sizeof(int));
	a=realloc((int*) a,(n+m)*sizeof(int));
	printf("Enter the elements of the list to be added to the original list:");
	for (j=0;j<m;j++)
	{
		scanf("%d",(b+j));
		*(a+n+j)=*(b+j);
	}
	printf("The new list is:");
	for (t=0;t<m+n;t++)
	{
		printf(" %d ",*(a+t));
	}	
}

int sorting(int* a,int n)
{
	int k,t,i,j,temp;
	for (t=0;t<n;t++)
	{
		
		for (j=t+1;j<n;j++)
		{
			k=*(a+t);
			if (k> *(a+j))
			{
				temp=k;
				*(a+t)=*(a+j);
				*(a+j)=temp;			
			}
			else
			{
				continue;
			} 		
		}
	}
	printf("The sorted list is:");
	for (i=0;i<n;i++)
	{	
		printf(" %d ",*(a+i));
	}
}

void main()
{
	int *a,ans;
	int i,one,function_name;
	printf("Enter the original length of your array:");
	scanf("%d",&n);
	a=malloc(n*sizeof(int));
	printf("Enter the elements of your array in succession:\n");
	for (i=0;i<n;i++)
	{
		scanf("%d",(a+i));
	}
	do
	{
	printf("ENTER THE FUNCTION THAT IS TO BE IMPLEMENTED:\n1=indexing\n2=count\n3=reverse\n4=insert\n5=pop\n6=delete\n7=append\n8=extend\n9=sort\n");
	scanf("%d",&function_name);
	switch (function_name)
	{	
		case 1:indexing(a,n);
			break;
		case 2:count(a,n);
			break;
		case 3:reverse(a,n);
			break;
		case 4:insert(a,n);	
			break;
		case 5:pop(a,n);
			break;
		case 6:delete(a,n);
			break;	
		case 7:append(a,n);
			break;	
		case 8:extend(a,n);
			break;	
		case 9:sorting(a,n);
			break;
	}
	printf("Do you wish to continue?\nType 1 if Yes and 2 if no.\n");
	scanf("%d",&ans);
	}while (ans==1);
}






